package m2;

import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer {
    public static void main(String[] args) throws Exception {
        // 连接消息服务器
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");   // wht6.cn
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();

        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                String msg = new String(message.getBody());
                System.out.println("收到"+msg);
                for (int i = 0 ; i < msg.length(); i++ ){
                    if (msg.charAt(i) =='.'){
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                //手动发送回执
                c.basicAck(message.getEnvelope().getDeliveryTag(),false);
                System.out.println("---------------------消息处理结束\n");
            }
        };
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {

            }
        };
        // 定义队列
        c.queueDeclare("task_queue",true,false,false,null);

            c.basicQos(1);
        c.basicConsume("task_queue", false, deliverCallback, cancelCallback);


    }
}
